package weaver.interfaces.linearin.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.linearin.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-12-05
 * @Description: 借款审批（先积）流程归档后调用生成凭证接口
 * @ModifyLog:
 **/
public class WeaJksplcSYBAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaJksplcSYBAction>>>>>requestid=" + requestid);
        try {
            JSONObject dataJson = new JSONObject();
            String message = "调用金蝶系统接口返回:";
            RecordSet rs = new RecordSet();
            ResourceComInfo comInfo = new ResourceComInfo();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String sqr = Util.null2String(rs.getString("sqr"));
            String zfrq = Util.null2String(rs.getString("zfrq"));
            String jdrybm = Util.null2String(rs.getString("jdrybm"));
            String jdbmbm = Util.null2String(rs.getString("jdbmbm"));
            double jkje = Util.getDoubleValue(rs.getString("jkje"));
            String zfzhd = Util.null2String(rs.getString("zfzhd"));
            String bookid = Util.null2String(rs.getString("bookid"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            if (!"".equals(bookid)) {
                //单据体：FEntity
                JSONArray FEntity = new JSONArray();
                /*单据体第一行*/
                boolean flag1 = jkje > 0;
                if (flag1) {
                    JSONObject FEntityJSON = new JSONObject();
                    //摘要
                    String fexplanation = lcbh+"-"+comInfo.getLastname(sqr) + "-借备用金";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "1221.03");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }

                    //金蝶人员编码
                    if (!"".equals(jdrybm)) {
                        JSONObject FDETAILID__FFLEX7 = new JSONObject();
                        FDETAILID__FFLEX7.put("FNumber", jdrybm);
                        FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", jkje);
                    FEntity.add(FEntityJSON);
                }
                /*单据体第二行*/
                boolean flag2 = jkje > 0;
                if (flag2) {
                    //摘要
                    JSONObject FEntityJSON = new JSONObject();
                    String fexplanation = lcbh+"-"+comInfo.getLastname(sqr) + "-借备用金";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "1002.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //银行账号
                    JSONObject FDETAILID__FF100006 = new JSONObject();
                    FDETAILID__FF100006.put("FNumber", zfzhd);
                    FDetailID.put("FDETAILID__FF100006", FDETAILID__FF100006);
                    FEntityJSON.put("FDetailID", FDetailID);
                    //贷方金额
                    FEntityJSON.put("FCREDIT", jkje);
                    FEntity.add(FEntityJSON);
                }
                dataJson.put("zfrq", zfrq);
                dataJson.put("FEntity", FEntity);
                writeLog(">>>>>WeaJksplcSYBAction>>>>>dataJson=" + dataJson.toJSONString());
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaJksplcSYBAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("bookid", bookid);
                dataMap.put("cz", src);
                JSONObject resultJson = KingdeeUtil.passCertificate(dataJson, dataMap);
                writeLog(">>>>>WeaJksplcSYBAction>>>>>resultJson=" + resultJson.toJSONString());
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String dataStr = Util.null2String(resultJson.getString("data"));
                    JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                    JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                    JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                    String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                    if ("false".equals(IsSuccess)) {
                        message += Util.null2String(ResponseStatusJson.getString("Errors"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}